<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a><cite>活动管理</cite></a>
    <a><cite>合成</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layadmin-search">
      <form class="layui-form" method="post" lay-filter="form_convert_search">
        <script type="text/html" template>
          <div class="layui-inline">
            <div class="layui-form-label">关键字搜索</div>
            <div class="layui-input-inline">
              <input type="text" name="keyword" value="{{decodeURIComponent(layui.router().search.keyword || '')}}"
                     class="layui-input"
                     placeholder="请输入关键字">
            </div>
          </div>
          <div class="layui-inline layui-btn-group">
            <button type="submit" lay-submit class="layui-btn" lay-filter="submit_convert_search">
              <i class="layui-icon layui-icon-search"></i>查询
            </button>
            <button type="submit" lay-submit class="layui-btn layui-btn-warm"
                    lay-filter="reset_convert_search">
              <i class="layui-icon layui-icon-close"></i>重置
            </button>
<!--            <a lay-href="convert/form" class="layui-btn layui-btn-normal">-->
<!--              <i class="layui-icon layui-icon-add-1"></i>添加合成公式-->
<!--            </a>-->
          </div>
        </script>
      </form>
    </div>
    <div class="layui-card-body">
      <div id="table_convert"></div>
      <script type="text/html" id="tpl_status">
        {{# if (d.status.value === 'disabled') { }}
        <span class="layui-font-gray">{{d.status.description}}</span>
        {{# } }}
        {{# if (d.status.value === 'enabled') { }}
        <span class="layui-font-green">{{d.status.description}}</span>
        {{# } }}
      </script>
      <script type="text/html" id="tpl_target">
        {{d.target.name}}
      </script>

      <script type="text/html" id="tpl_agent">
        {{d.agent ? d.agent.name : ''}}
      </script>

      <script type="text/html" id="tpl_demands">
        <a class="layui-table-link" lay-event="show_demands">查看要求</a>
      </script>
      <script type="text/html" id="tpl_records">
        <a class="layui-table-link" lay-event="show_records">查看记录（{{d.records_count}}）</a>
      </script>
      <script type="text/html" id="tpl_tool">
        <a class="layui-table-link layui-font-green" lay-href="convert/form/id={{d.id}}">
          编辑
        </a>
        {{# if (d.status.value === 'enabled') { }}
        <div class="ant-divider"></div>
        <a class="layui-table-link layui-font-red" lay-event="disable">
          禁用
        </a>
        {{# } }}
        {{# if (d.status.value === 'disabled') { }}
        <div class="ant-divider"></div>
        <a class="layui-table-link layui-font-green" lay-event="enable">
          启用
        </a>
        {{# } }}
      </script>
    </div>
  </div>
</div>

<script>
  layui.use(['fox_table'], function () {
    let $ = layui.$
      , fox_table = layui.fox_table
      , admin = layui.admin
      , form = layui.form
      , view = layui.view

    let resourceUrl = 'admin/convert_formulas'

    // 列表查询
    fox_table.render({
      id: 'table_convert'
      , url: resourceUrl
      , cols: [[
        {title: 'ID', field: 'id', width: 80, align: "center", sort: true}
        , {title: '所属代理', templet: '#tpl_agent', width: 150}
        , {title: '合成名称', field: 'name', minWidth: 100}
        , {title: '合成目标', templet: '#tpl_target', width: 200}
        , {title: '合成要求', templet: '#tpl_demands', width: 100}
        , {title: '已合成数量', templet: '#tpl_records', width: 150, align: "center"}
        , {title: '状态', templet: '#tpl_status', width: 100, align: "center"}
        , {title: '添加时间', field: 'created_at', width: 180, align: "center"}
        // , {title: '操作', width: 150, align: 'left', fixed: 'right', toolbar: '#tpl_tool'}
      ]]
    });

    //监听操作栏
    fox_table.toolbar(function (obj) {
      let data = obj.data;
      if (obj.event === 'enable') {
        layer.confirm('确定启用该合成公式?', function (index) {
          admin.post(resourceUrl + '/' + data.id + '/status', {status: 'enabled'}, function (res) {
            layer.close(index);
            fox_table.reload(); //重载表格
          })
        });
      }
      if (obj.event === 'disable') {
        layer.confirm('确定禁用该合成公式?', function (index) {
          admin.post(resourceUrl + '/' + data.id + '/status', {status: 'disabled'}, function (res) {
            layer.close(index);
            fox_table.reload(); //重载表格
          })
        });
      }
      if (obj.event === 'show_demands') {
        let demands = []
        layui.each(data.demands, function (index, demand) {
          demands.push(demand.product.name + ', 数量: ' + demand.quantity)
        })
        layer.msg(demands.join('<br/>'))
      }
      if (obj.event === 'show_records') {
        showUserRegistry(data)
      }
    });

    //搜索
    form.on('submit(submit_convert_search)', function (d) {
      fox_table.reload(d.field);
      return false;
    });
    //重置
    form.on('submit(reset_convert_search)', function (d) {
      $("input[name='keyword']").val('');
      fox_table.reload({});
      return false;
    });

    //显示合成记录
    function showUserRegistry(data) {
      let minWidth = '900px';
      let minHeight = '730px';
      admin.popup({
        title: '合成记录'
        , area: [minWidth, minHeight]
        , id: 'popup_convert_records'
        , success: function (layero, index) {
          view(this.id).render('convert/records', data).done(function () {
          })
        }
      })
    }
  })
</script>

